﻿package Space.View 
{
	
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import GameEvents;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.scenes.Scene3D; 
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.osflash.thunderbolt.Logger;
	import Utils.WebServiceCommunication;
	import Utils.StringParser;
	import Space.SpaceObjects.Star;
	import Space.SpaceObjects.Fleet;
	
	public class MapView 
	{
		
		private var _mapView:Scene3D;
		private var _cameraObject:DisplayObject3D;
		private var _stars:Array;
		private var WSMap:String;
		private var starArray:Array;
		private var tempFleet:Space.SpaceObjects.Fleet;
		
		public function MapView(_fleet:Fleet):void 
		{
			tempFleet = _fleet;
			var WSC:WebServiceCommunication = new WebServiceCommunication();
			WSC.MapPosition(_fleet.x, _fleet.y, _fleet.z,_fleet.RadarRadius);
			WSC.addEventListener("MapViewData", MapData);
			_mapView 		= new Scene3D();
			_cameraObject 	= new DisplayObject3D();
		}
		
		public function MapData(e:GameEvents):void
		{
			WSMap = e.command.toString();
			var stringPar:StringParser = new StringParser();
			_stars = stringPar.MapString(WSMap);
			BuildMap();
			Logger.debug("Test", starArray[0]);
		}
		
		public function BuildMap():void 
		{
			var filter:GlowFilter = new GlowFilter(0xFFFF16, 1, 10, 10, 5, 2);
			var blur:BlurFilter = new BlurFilter(2,2,1);
			var object3D:Sphere;
			var starMaterial:ColorMaterial 	= new ColorMaterial(0xFFFF16);
			var fleetMaterial:ColorMaterial = new ColorMaterial(0xFF0509);
			starMaterial.interactive 	= true;
			fleetMaterial.interactive 	= true;
			for (var i:Number = 0; i < _stars.length; i++ ) {
				object3D = new Sphere( starMaterial, 5, 10, 10);
				object3D.name = i.toString();
				object3D.useOwnContainer = true;
				object3D.filters = [filter,blur];
				object3D.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, ObjectOver);
				object3D.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, ObjectOut);
				object3D.x = (_stars[i].x - tempFleet.x)*50;
				object3D.y = (_stars[i].y - tempFleet.y)*50;
				object3D.z = (_stars[i].z - tempFleet.z)*50;
				object3D.useOwnContainer = true;
				_mapView.addChild(object3D);
			}
			
			object3D = new Sphere( fleetMaterial, 5, 10, 10);
			object3D.name = (String)(i+1);
			_cameraObject = object3D;
			object3D.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, ObjectOver);
			object3D.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, ObjectOut);
			object3D.x = 0;
			object3D.y = 0;
			object3D.z = 0;
			object3D.useOwnContainer = true;
			_mapView.addChild(object3D);

		}
		
		private function ObjectOut(e:InteractiveScene3DEvent):void 
		{
			//_mapView.dispatchEvent( new GameEvents(_objects[Number(e.displayObject3D.name)], "Object_Out"));
		}
		
		private function ObjectOver(e:InteractiveScene3DEvent):void 
		{
			//_mapView.dispatchEvent( new GameEvents(_objects[Number(e.displayObject3D.name)], "Object_Over"));
		}
		
		public function get Map():Scene3D 
		{
			return _mapView;
		}
		
		public function get cameraObject():DisplayObject3D 
		{
			return _cameraObject;
		}
	}
	
}